/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.sdMiningChallenge;

import com.SySammy.sdMiningChallenge.Listeners.sdMinningListener;
import com.SySammy.SdCurrency.sdCCommandExport;
import com.SySammy.sdMiningChallenge.prop.MiningProperties;
import java.util.HashMap;
import java.util.Set;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Player;

public class challengeMethods {

    private static sdCCommandExport CC = new sdCCommandExport();
    public static HashMap<String, Integer> scores = new HashMap<String, Integer>();
    public static boolean started = false;
    public static boolean joining = false;
    public static Integer[] time;
    public static long TimeLeft;
    public static long TimeLeft1970;
    public static int qnt;
    public static int prize;
    public static int entryfee;
    public static int ChallengeSchedule;
    public static int ChallengeAnnouncer;
    public static String maker;
    public static Player winner;
    public static sdMC plugin;
    public static String[] ores;
    public static Location loc;
    public static int entryAccount;

    public void SetChallenge(String ply, final sdMC plugin, Location loc, final String mat, final int qnt, final int entryfee, final int Gprize) {
        challengeMethods.plugin = plugin;
        challengeMethods.entryfee = entryfee;
        challengeMethods.maker = ply;
        sdMinningListener.mat = mat;
        challengeMethods.qnt = qnt;
        sdMinningListener.qnt = qnt;
        challengeMethods.loc = loc;
        challengeMethods.prize = Gprize;
        challengeMethods.joining = true;
        loc.setY(loc.getY() + 1);

        plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

            public void run() {
                if (scores.size() >= MiningProperties.MinPlayers) {
                    startChallenge(time(mat));
                    joining = false;
                } else {
                    ReturnEntryFee();
                    joining = false;
                    CC.GiveMoney(maker, prize + MiningProperties.challengeCost);
                    msgPly(ChatColor.RED + "[MC]: Mining challenge aborted:" + ChatColor.GOLD + " not enough players");
                    StopChallenge();
                }
            }
        }, MiningProperties.WaitingTime * 60 * 20L);
    }

    public Integer[] time(String mat) {
        if (mat.equalsIgnoreCase("COAL_ORE")) {
            Integer[] times = {60, 30};
            return times;
        }
        if (mat.equalsIgnoreCase("IRON_ORE")) {
            Integer[] times = {120, 60};
            return times;
        }
        if (mat.equalsIgnoreCase("GOLD_ORE")) {
            Integer[] times = {180, 210};
            return times;
        }
        if (mat.equalsIgnoreCase("REDSTONE_ORE")) {
            Integer[] times = {240, 90};
            return times;
        }
        if (mat.equalsIgnoreCase("DIAMOND_ORE")) {
            Integer[] times = {300, 300};
            return times;
        }
        if (mat.equalsIgnoreCase("LAPIS_ORE")) {
            Integer[] times = {600, 600};
            return times;
        }
        return null;
    }

    public void JoinChallenge(Player ply) {
        if (ply != null) {
            CC.TakeMoney(ply.getName(), entryfee);
            entryAccount = entryAccount + entryfee;
            scores.put(ply.getName(), 0);
            ply.teleport(loc);
            ply.sendMessage(ChatColor.DARK_AQUA + "[MC]: You joined the mining challenge, be ready to start.");
        }
    }

    public void ReturnEntryFee() {
        if (scores != null) {
            Set<String> keySet = scores.keySet();
            for (int x = 1; x <= scores.size(); x++) {
                CC.GiveMoney(keySet.iterator().next(), entryfee);
            }
        }
    }

    public void msgPly(String msg) {
        Player makeply = plugin.getServer().getPlayer(maker);
        if (makeply != null) {
            makeply.sendMessage(msg);
        }
        Object[] players = scores.keySet().toArray();
        if (players != null) {
            for (int x = 0; x < scores.size(); x++) {
                Player ply = plugin.getServer().getPlayer(players[x].toString());
                if (ply != null) {
                    ply.sendMessage(msg);
                }
            }
        }
    }

    private void startChallenge(final Integer[] times) {
        time = times;
        time[1] = time[1] * qnt;
        TimeLeft1970 = (System.currentTimeMillis());
        TimeLeft = time[0];
        started = true;
        plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

            public void run() {
                ChallengeSchedule = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

                    public void run() {
                        StopChallenge();
                        plugin.getServer().getScheduler().cancelTask(ChallengeSchedule);
                        plugin.getServer().getScheduler().cancelTask(ChallengeAnnouncer);
                        plugin.getServer().broadcastMessage(ChatColor.DARK_AQUA + "[MC]: Times up, no one won the mining challenge.");
                    }
                }, (time[1] * 20L));
            }
        }, time[0] * 20L);

        ChallengeAnnouncer = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {

            public void run() {
                msgPly(ChatColor.DARK_AQUA + "[MC] Don't forget to check the time!!!");
                msgPly(ChatColor.GREEN + "Scores: " + ChatColor.GOLD + challengeMethods.scores.toString());
                msgPly(ChatColor.GREEN + "Time Left: " + ChatColor.GOLD + timeLeft() + " sec.");
            }
        }, 60 * 21L, 60 * 21L);
        msgPly(ChatColor.DARK_AQUA + "[MC]:" + ChatColor.GOLD + " Mining Challenge started, grab your pickaxe!!");
        CC.GiveMoney(maker, entryAccount);
    }

    public long timeLeft() {
        TimeLeft = ((time[0] + time[1]) - ((System.currentTimeMillis() - TimeLeft1970) / (1000)));
        return TimeLeft;
    }

    public void StopChallenge() {
        scores.clear();
        sdMinningListener.mat = "";
        sdMinningListener.qnt = 0;
        entryAccount = 0;
        started = false;
        plugin.getServer().getScheduler().cancelTask(ChallengeSchedule);
        plugin.getServer().getScheduler().cancelTask(ChallengeAnnouncer);
    }

    public void GivePrize() {
        CC.GiveMoney(winner.getName(), prize);
        plugin.getServer().broadcastMessage(ChatColor.DARK_AQUA + "[MC]: Challenge is over, " + ChatColor.GOLD + winner.getName() + " own.");
        if (winner != null) {
            winner.sendMessage(ChatColor.DARK_AQUA + "[MC]: Congrats you own the mining challenge");
            winner.sendMessage(ChatColor.DARK_AQUA + "Prize: " + ChatColor.GOLD + prize + " " + CC.GetCurrency());
        }
        StopChallenge();
    }

    public boolean UsableOre(String ore) {
        ores = MiningProperties.ores.split(";");
        for (int x = 0; x < ores.length; x++) {
            if (ore.equalsIgnoreCase(ores[x])) {
                return true;
            }
        }
        return false;
    }
}
